package com.lisp.springframework.core.aop.springaop;

import org.springframework.aop.framework.Advised;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.PropertySource;

@Configuration
@EnableAspectJAutoProxy 
@PropertySource("classpath:app.properties")
@ComponentScan(basePackages = "com.lisp.springframework.core.aop.springaop")
public class AopApp {
    public static void main(String[] args) {
        try(AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();) {            
            ctx.register(AopApp.class);
            ctx.refresh();
            MyServiceA myServiceA = ctx.getBean(MyServiceA.class);
            
            String myServiceAStr = myServiceA.toString();
            System.out.println(myServiceAStr);
            
            Advised advised = (Advised) myServiceA;
            System.out.println("isFrozen: " + advised.isFrozen());
            
            myServiceA.sayHello("lisp"); 
            
            UsageTracked usageTracked = ctx.getBean("myServiceA", UsageTracked.class);
            System.out.println("usage tracked: " + usageTracked.get());
            
            System.out.println(ctx.getBean(AopApp.class));
            
            // 内部转调，不会触发切面
            InnerInvoke innerInvoke = ctx.getBean(InnerInvoke.class);
            innerInvoke.sayHello("innerInvoke");
        }
    }
}
